using System;
using System.Runtime.InteropServices;
using System.Security.Authentication;
using System.Windows;
using System.Windows.Input;
using System.Windows.Threading;

using HabilisDesign.Iota.Properties;

namespace HabilisDesign.Iota
{
	/// <summary>
	/// Interaction logic for MainWindow.xaml
	/// </summary>

	public partial class MainWindow : System.Windows.Window
	{
		private StatusCollection m_CurrentCollection;
		private TwitterGateway m_Gateway;
		private DispatcherTimer m_Timer;

		public MainWindow()
		{
			InitializeComponent();
			m_CurrentCollection = new StatusCollection();
			StatusViewer.ItemsSource = m_CurrentCollection;
			m_Timer = new DispatcherTimer();
			m_Timer.Tick += new EventHandler( UpdateCollection );
			m_Timer.Interval = TimeSpan.FromSeconds( Settings.Default.PollingPeriod );
			if ( Settings.Default.Username.Length == 0 || Settings.Default.Password.Length == 0 )
            {
				ShowSettings( this, new RoutedEventArgs() );
            }
			else
			{
				InitializeGateway();
			}
		}

		private void CheckForSubmit( object sender, KeyEventArgs e )
		{
			if ( e.Key == Key.Enter )
			{
				SubmitStatus( sender, new RoutedEventArgs() );
				e.Handled = true;
			}
		}

		private void SubmitStatus( object sender, RoutedEventArgs e )
		{
			string updateText = UpdateBox.Text.Trim();
			if ( string.IsNullOrEmpty( updateText ) || CheckDefaultText() )
			{
				return;
			}
			m_Gateway.SendStatusUpdate( updateText );
			UpdateBox.Clear();
		}

		private bool CheckDefaultText()
		{
			return UpdateBox.Text.Trim() == UpdateBox.ToolTip.ToString();
		}

		private void InitializeGateway()
		{
			if ( m_Gateway == null )
			{
				m_Gateway = new TwitterGateway( Settings.Default.Username, Settings.Default.Password );
			}
			else
			{
				m_Gateway.Username = Settings.Default.Username;
				m_Gateway.Password = Settings.Default.Password;
				m_CurrentCollection.Clear();
			}
			try
			{
				m_Gateway.InitializeCollection( m_CurrentCollection );
				m_Timer.Start();
			}
			catch ( AuthenticationException ex )
			{
				MessageBox.Show( ex.Message );
				ShowSettings( this, new RoutedEventArgs() );
			}
		}

		private void UpdateCollection( object sender, EventArgs e )
		{
			m_Gateway.Update( m_CurrentCollection );
		}

		private void ShowSettings( object sender, RoutedEventArgs e )
		{
			SettingsWindow settings = new SettingsWindow();
			bool? updated = settings.ShowDialog();
			if ( updated.Value )
			{
				InitializeGateway();
			}
		}

		private void Close( object sender, RoutedEventArgs e )
		{
			m_Timer.Stop();
			Dispatcher.InvokeShutdown();
			try
			{
				this.Close();
			}
			catch ( COMException )
			{
				// Sucks here too.
			}
		}

		private void Minimize( object sender, RoutedEventArgs e )
		{
			this.WindowState = WindowState.Minimized;
		}

		private void BeginMove( object sender, MouseButtonEventArgs e )
		{
			try
			{
				this.DragMove();
			}
			catch ( COMException )
			{
				// Swallow this for now.
			}
		}

		private void NavigateHome( object sender, RoutedEventArgs e )
		{
			// TODO: Open browser window to home location.
		}

	} // end class

} // end namespace